'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会操作
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var Code = require('../../../../consts/code');
var roleModel = require('../../../../models/roleModel');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');

/**
 * 解散公会-----and------退出公会   PS: 是公长达么条件，解散公会，如果是成员退出公会
 */
module.exports = function (selfGlobal, selfApp, msg, session, next) {
    var role_id = session.uid;
    var union_id = 0;
    var role_model;
    var union_group_model;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'union_task', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.getUnionId();
                if (!!union_id) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_group_model = model;
                        cb();
                    });
                } else {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
            },
            function (cb) {
                if (!!union_group_model) {
                    var union_group_list = union_group_model.getUnionUserList();
                    var union_num = union_group_list.length;
                    var position = 0;
                    var union_task_model = player_manager.union_task;

                    _.each(union_group_list, function (group_item) {
                        if (group_item.role_id == role_id) {
                            position = group_item.position;

                        }
                    });

                    // 判断玩家是否加入公会
                    if (!role_model.union_id) {
                        return next(null, {
                            code: Code.UNION_IS_NOT_JOIN
                        });
                    }

                    if (position == 1) {
                        // 公会是否还有成员
                        if (union_num > 1) {
                            return next(null, {
                                code: Code.UNION_HAVE_OTHER_MEMBER
                            });
                        }

                        // 清空历史声望
                        union_task_model.resHistory(0);
                        role_model.setUnionId(0);

                        // 角色表设置联盟信息
                        role_model.union_name = '';
                        role_model.union_flag = 0;

                        // 解散联盟(公会长)
                        unionGroupModel.dismiss(union_id, cb);
                    } else {

                        // 退出公会(普通成员)
                        union_group_model.removeUnionUser(role_id);
                        role_model.setUnionId(0);

                        // 角色表设置联盟信息
                        role_model.union_name = '';
                        role_model.union_flag = 0;

                        // 清空历史声望
                        union_task_model.resHistory(0);
                    }
                }

                // 移除联盟聊天聊天通道
                selfApp.rpc.connector.entryRemote.removeUnionChannel(session, union_id, role_id, function (err) {
                    if (!!err) {
                        console.err(err);
                    }
                });

                cb();
            },
            function (cb) {
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            if (!!union_group_model) {
                union_group_model.save();
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
